package com.tangxz.chat.modules.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tangxz.chat.common.cache.UserRedisCache;
import com.tangxz.chat.common.dto.UserRedisDto;
import com.tangxz.chat.common.enums.UserState;
import com.tangxz.chat.common.utils.R;
import com.tangxz.chat.modules.user.entity.TUser;
import com.tangxz.chat.modules.user.service.ITuserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 用户表
 * @Author: 唐小尊
 * @Date: 2021-04-20 09:48:24
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "用户表")
@RestController
@RequestMapping("/tuser")
public class TuserController {
    @Autowired
    private ITuserService tuserService;
    @Autowired
    private UserRedisCache userRedisCache;

    /**
     * 得到全部非自己的用户
     *
     * @return
     */
    @ApiOperation(value = "用户表-得到全部非自己的用户", notes = "用户表-得到全部非自己的用户")
    @PostMapping(value = "/getAllUser/{userId}")
    public R getAllUser(@PathVariable("userId") String userId) {
        List<TUser> allUser = tuserService.getAllUser(userId);
        return R.ok().put("allUser",allUser);
    }

    /**
     * 登录
     *
     * @return
     */
    @ApiOperation(value = "用户表-登录", notes = "用户表-登录")
    @PostMapping(value = "/login")
    public R login(@RequestBody TUser tuser) {
        TUser user = tuserService.login(tuser);
        if (user == null) {
            return R.error("登录异常");
        }
        //前端实现发起建立连接请求
        return R.ok().put("user", user);
    }

    /**
     * 分页列表查询
     *
     * @return
     */
    @ApiOperation(value = "用户表-分页列表查询", notes = "用户表-分页列表查询")
    @GetMapping(value = "/allUser")
    public R list() {
        List<TUser> list = tuserService.list();
        return R.ok().put("list", list);
    }

    /**
     * 得到某个登录用户基本信息
     *
     * @return
     */
    @ApiOperation(value = "用户表-得到某个登录用户基本信息", notes = "用户表-得到某个登录用户基本信息")
    @PostMapping(value = "/getOneUserInfo/{userId}")
    public R getOneUserInfo(@PathVariable("userId") String userId) {
        return R.ok().put("user", userRedisCache.getOneByUserId(userId));
    }

    /**
     * 得到全部已登录
     *
     * @return
     */
    @ApiOperation(value = "用户表-得到全部已登录", notes = "用户表-得到全部已登录")
    @PostMapping(value = "/logout")
    public void logout(@RequestBody String userId) {
        UserRedisDto oneByUserId = userRedisCache.getOneByUserId(userId);
        oneByUserId.setState(UserState.OFFLINE.type);
        userRedisCache.save(userId, oneByUserId);
        //前端实现发起建立连接请求
    }

}
